package com.group4.edu.controller;


import com.group4.edu.entity.Chapter;
import com.group4.edu.service.ChapterService;
import com.study.util.LogUtils;
import com.study.util.ResponseResult;
import com.study.vo.ChapterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/edu/chapter")
@Api(description = "章节管理")
@CrossOrigin
@Slf4j
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    @ApiOperation(value = "测试")
    @GetMapping("/test")
    public ResponseResult<String> test(
            @ApiParam(name = "i",value = "测试数据",required = true) Integer i){
        return new ResponseResult<>(200,"test",i+"");
    }

    @ApiOperation(value = "获取章节")
    @GetMapping("nested-list/{courseId}")
    public ResponseResult getChapterByCourseId(
            @ApiParam(value = "课程id",required = true)
            @PathVariable String courseId){
        LogUtils.info(log,"查课程章节带小节",courseId);
        ResponseResult<Object> responseResult = new ResponseResult<>(200,"suc");
        List<ChapterVO> chapterVOList = chapterService.selectChapterAndVideoByCourseId(courseId);
        responseResult.setData(chapterVOList);
        return responseResult;
    }

    @ApiOperation("添加chapter")
    @PostMapping
    public ResponseResult addChapter(@RequestBody Chapter chapter){
        LogUtils.info(log,"添加章节",chapter);
        chapterService.save(chapter);
        return ResponseResult.SUCCESS;
    }

    @ApiOperation("删除chapter")
    @DeleteMapping("{id}")
    public ResponseResult deleteChapter(@PathVariable String id){
        LogUtils.info(log,"删除chapter",id);
        chapterService.deleteChapterById(id);
        return ResponseResult.SUCCESS;
    }

    @ApiOperation("查询chapter")
    @GetMapping("{id}")
    public ResponseResult getChapter(@PathVariable String id){
        LogUtils.info(log,"查询chapter",id);
        Chapter c = chapterService.getById(id);
        return new ResponseResult(c);
    }

    @ApiOperation("修改chapter")
    @PutMapping
    public ResponseResult updateChapter(@RequestBody Chapter chapter){
        LogUtils.info(log,"更新章节",chapter);
        chapterService.updateById(chapter);
        return ResponseResult.SUCCESS;
    }
}

